/*
 * redmicro all Copyright (c)
 */

/**
 * Created by seeker910 on 2014/8/13.
 */
Rsd.define('Rsd.control.RichTextBox', {
    extend: 'Rsd.control.Component',
    xtype: 'rich-text-box',
    //height: 30,
    margin: '3 5 10 5',
    label:{position:'top',content:null,height:45},
    labelTagName:'div',
    ctrlTagName: 'textarea',
    style:{textAlign:'left'}, 
    readonly: false,  
    autoLoad:true,
    contentType:"text/html",
    "static":function __init() {

         /*
            var nicEditURL = Rsd.getRedjsHost() + 'plugin/nicEdit/nicEdit.js';
            //Rsd.loadPlugin：向dom中添加<script>标签，来加载插件
            //Rsd.loadPlugin(nicEditURL);
            //Rsd.loadScriptFile直接加载js文件
            Rsd.loadScriptFile(nicEditURL);
        */

        Rsd.richTxtProvider={provider:null,isLoading:true,cache:[]};
 
        var count =0;

        var _fn = function(){

            count++;
            if(count > 40)
            { 
                if (window.nicEditor == null) {
    
                    if(Rsd.app.useRichText)
                    {
                        Rsd.alert(Rsd.lang('control.richtext.provider_not_exist'));
                        console.log('(已延时'+count/2+'秒)请在启动代码里，添加以下代码： Rsd.loadPlugin("plugin/nicEdit/nicEdit.js", function () { });');
                    }  
                    return;
                } 
            }

            if(window.nicEditor == null)
            {
                setTimeout(_fn,500); 
                return;
            }

            Rsd.richTxtProvider.provider = window.nicEditor;
            for(var i in Rsd.richTxtProvider.cache)
            {
                Rsd.richTxtProvider.cache[i].loadData();
            }
            Rsd.richTxtProvider.cache=[];
            Rsd.richTxtProvider.isLoading = false;

        } 

        _fn();
    },
    /**
     * 
     * @param {*} config 
     */
    constructor: function RichTextBox(config) {
        config = config || {};
        this.apply(config); 
    },
    /**
     * 
     */
    onAfterInit:function onAfterInit()
    {   
        var me = this;
        this.callParent(); 
        Rsd.loadPlugin("plugin/nicEdit/nicEdit.js", function () {  
            me.fire('pluginLoaded');  
         }); 
    },
    /**
     * 
     */
    onAfterRender:function onAfterRender()
    {
        var me = this;
        me.callParent();
        me.ctrl.element.style.resize='none'; 
 
        var _fn = function(){

            setTimeout(function () {
                var editor = new nicEditor({
                    fullPanel: true,
                    iconsPath: Rsd.getRedjsHost() + '/plugin/nicEdit/nicEditorIcons.gif'
                }).panelInstance(me.ctrl.element.id);

                if (me.readonly) {
                    editor.commands && editor.commands.save && editor.commands.save.disable();
                }
                else {
                    editor.commands && editor.commands.save && editor.commands.save.enable();
                }
                me.editor = editor;
            }, 1000); 
            
        };

        if(window.nicEditor)
        {
            _fn(); 
        }
        else
        {
            me.on('pluginLoaded',_fn)
        }
         
    },
   
     /*
     设置为只读
     * */
    setReadOnly:function setReadOnly(readonly) {

        this.ctrl.element.setAttribute('disabled',readonly);
        this.ctrl.element.setAttribute("readonly",readonly);  
        this.readonly = readonly;

        var editor = this.editor; 

        if(editor)
        {  
            if(readonly)
            { 
                editor.commands&&editor.commands.save&&editor.commands.save.disable();
            }
            else
            {
                editor.commands&&editor.commands.save&&editor.commands.save.enable();
            } 
        } 
        
    },
    

    /*
    * */
    setUIColor:function(c)
    {
        var editor = this.editor;

        if(editor)
        {
            editor.setUiColor(c);
        }

    },
    /*
    *
    * */
    getValue:function getValue() {
        if(this.editor && this.editor.nicInstances[0])
        {
            return this.editor.nicInstances[0].getContent();
        } 
        return this.ctrl.element.value;
    },
    /*
    * */
    setValue:function setValue(data) {

        var me = this;
        var _fn = function()
        {
             if(Rsd.isString(data))
            { 
                if(me.contentType==='text/html'||this.contentType==='TEXT/HTML')
                {
                    me.ctrl.element.value = data.replace('\n','<br>').replace('\r','<br>'); 
                } 
                else
                {
                    me.ctrl.element.value = data;
                } 
            }
            else
            {
                me.ctrl.element.value = '';
            }
        }
          
        if (window.nicEditor == null) {
    
            me.on('pluginLoaded',_fn);
            //Rsd.alert(Rsd.lang('control.richtext.provider_not_exist'));
            //console.log('请在启动代码里(或RichTextBox控件使用前)，添加以下代码： Rsd.loadPlugin("plugin/nicEdit/nicEdit.js", function () { });');
            return;
        }  

        _fn();
       
        
    },
    /*
    * */
    remove:function remove() {
        this.callParent(); 
    },
  

},function(type){

   
    var _editorGetter = function () {
 
        return  this.__editor;
    };
    var _editorSetter = function (editor) {
        this.__editor = editor;
       return ;
    };

    this.defineProperty(type,"editor", _editorGetter, _editorSetter,true);

});
